package online.inote.common.configuration;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * <p>
 * Desc: Mybatis(二)配置
 * </p>
 *
 * @author XQF SUI
 * @created 2017年12月11日 下午9:21:42
 * @version 1.0
 */
@Configuration
@MapperScan(basePackages = {"online.inote.modules.user"}, sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveMybatisConfiguration {

	@Autowired
    @Qualifier("slaveDataSource")
    private DataSource dataSource;
	
	@Bean(name = "slaveSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource); // 使用business数据源
        return bean.getObject();
    }

    @Bean(name = "slaveSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory()); // 使用上面配置的Factory;
    }
    
    @Bean(name = "slaveTransactionManager")
    public PlatformTransactionManager slaveTransactionManager() {
     return new DataSourceTransactionManager(dataSource);
    }
}
